import { Config } from '../../utils/conf.js';
import { httpReq } from '../../utils/http.js';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    // 是否显示相关的按钮
    show: false,
    // 头像
    avatar: "https://www.caoshen.xyz/icon/logo.jpg",
    nick: '请先登录'
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // 判断是否授权过
    if (wx.getStorageSync("avatar")) {
      // 设置相关的数据
      this.setData({
        show: true,
        avatar: wx.getStorageSync("avatar"),
        nick: wx.getStorageSync("nick")
      });
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    return {
      title: '晓乎-知道你想知道的',
      desc: '我的问题在晓乎里找到了答案！',
      path: '/pages/area/area',
      imageUrl: 'https://www.caoshen.xyz/icon/logo.jpg',
      success:(res)=>{
        wx.showToast({
          title: '分享成功',
          icon: 'success',
        })
      }
    }
  },
  
  getinfo(evt){
    wx.setStorageSync("avatar", evt.detail.userInfo.avatarUrl);
    wx.setStorageSync("nick", evt.detail.userInfo.nickName);
    //给后台发用户信息
    let obj = {};
    obj.url = Config.userinfourl;
    obj.data = { openid: wx.getStorageSync('openid'), user: evt.detail.userInfo.nickName, pic: evt.detail.userInfo.avatarUrl};
    httpReq(obj).then(res => {
      wx.setStorageSync("uid", res.data.data);
      this.setData({
        show: true,
        avatar: evt.detail.userInfo.avatarUrl,
        nick: evt.detail.userInfo.nickName
      })
    })
  }
})